/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.rf.richfitwheel.common.utils;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeParseException;
import java.util.*;

public class DateUtils {
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     *
     * @param date 日期
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     *
     * @param date    日期
     * @param pattern 格式，如：DateUtils.DATE_TIME_PATTERN
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String format(Date date, String pattern) {
        if (date != null) {
            Locale locale1 = new Locale("zh", "CN");
            SimpleDateFormat df = new SimpleDateFormat(pattern, locale1);
            return df.format(date);
        }
        return null;
    }

    /**
     * 字符串转换成日期
     *
     * @param strDate 日期字符串
     * @param pattern 日期的格式，如：DateUtils.DATE_TIME_PATTERN
     */
    public static Date stringToDate(String strDate, String pattern) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }
        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        return fmt.parseLocalDateTime(strDate).toDate();
    }

    public static Date dateToDate(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        String strDate = format(date, pattern);
        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        return fmt.parseLocalDateTime(strDate).toDate();
    }

    /**
     * 根据周数，获取开始日期、结束日期
     *
     * @param week 周期  0本周，-1上周，-2上上周，1下周，2下下周
     * @return 返回date[0]开始日期、date[1]结束日期
     */
    public static Date[] getWeekStartAndEnd(int week) {
        DateTime dateTime = new DateTime();
        LocalDate date = new LocalDate(dateTime.plusWeeks(week));

        date = date.dayOfWeek().withMinimumValue();
        Date beginDate = date.toDate();
        Date endDate = date.plusDays(6).toDate();
        Calendar c = Calendar.getInstance();
        c.setTime(endDate);
        c = endTwoThree(c);
        return new Date[]{beginDate, c.getTime()};
    }

    public static Date getWeekStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, 2);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getWeekStart(String date) {
        Date inputDate = DateUtils.stringToDate(date, DATE_PATTERN);
        return getWeekStart(inputDate);
    }

    /**
     * 对日期的【秒】进行加/减
     *
     * @param date    日期
     * @param seconds 秒数，负数为减
     * @return 加/减几秒后的日期
     */
    public static Date addDateSeconds(Date date, int seconds) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusSeconds(seconds).toDate();
    }

    /**
     * 对日期的【分钟】进行加/减
     *
     * @param date    日期
     * @param minutes 分钟数，负数为减
     * @return 加/减几分钟后的日期
     */
    public static Date addDateMinutes(Date date, int minutes) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMinutes(minutes).toDate();
    }

    /**
     * 对日期的【小时】进行加/减
     *
     * @param date  日期
     * @param hours 小时数，负数为减
     * @return 加/减几小时后的日期
     */
    public static Date addDateHours(Date date, int hours) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusHours(hours).toDate();
    }

    /**
     * 对日期的【天】进行加/减
     *
     * @param date 日期
     * @param days 天数，负数为减
     * @return 加/减几天后的日期
     */
    public static Date addDateDays(Date date, int days) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusDays(days).toDate();
    }

    /**
     * 对日期的【周】进行加/减
     *
     * @param date  日期
     * @param weeks 周数，负数为减
     * @return 加/减几周后的日期
     */
    public static Date addDateWeeks(Date date, int weeks) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusWeeks(weeks).toDate();
    }

    /**
     * 对日期的【月】进行加/减
     *
     * @param date   日期
     * @param months 月数，负数为减
     * @return 加/减几月后的日期
     */
    public static Date addDateMonths(Date date, int months) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMonths(months).toDate();
    }

    /**
     * 对日期的【年】进行加/减
     *
     * @param date  日期
     * @param years 年数，负数为减
     * @return 加/减几年后的日期
     */
    public static Date addDateYears(Date date, int years) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusYears(years).toDate();
    }

    /**
     * 不考虑时分秒，对日期做减法，返回天数
     *
     * @param date1
     * @param date2
     * @return date1-date2
     */
    public static int compareToDays(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return -9999999;//返回异常值
        }
        boolean isF = false;//确保date1比date2大时，返回正数；date1比date2小时，返回负数；
        int resuls = 0;
        //如果year1比year2小，进行互换
        if (date1.compareTo(date2) == -1) {
            Date temp = date1;
            date1 = date2;
            date2 = temp;
            isF = true;
        } else if (date1.compareTo(date2) == 0) {//date1等于date2时返回0
            return 0;
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //不同年
        {
//            int timeDistance = 0;

            for (int i = year2; i < year1; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    resuls += 366;
                } else    //不是闰年
                {
                    resuls += 365;
                }
            }
            resuls = resuls + (day1 - day2);
        } else    //同一年
        {
            resuls = day1 - day2;
        }
        if (isF) {
            resuls = -resuls;
        }
        return resuls;
    }

    /**
     * 自定义日期拆分
     * 格式是"F2019-05-21T2019-05-29"
     *
     * @param date
     * @return List<Date> ， list.get(0)=startDate ,list.get(1)=endDate
     */
    public static List<Date> fromDateToDate(String date) {
        if (date == null) {
            return null;
        }
        List<Date> list = new ArrayList<Date>();
        Date startDate, endDate = null;
        startDate = stringToDate(date.substring(1, date.indexOf("T", 0)), DateUtils.DATE_PATTERN);
        endDate = stringToDate(date.substring(date.indexOf("T", 0) + 1, date.length()), DateUtils.DATE_PATTERN);
        Calendar c = Calendar.getInstance();
        c.setTime(endDate);
        c = endTwoThree(c);
        list.add(startDate);
        list.add(c.getTime());
        return list;
    }

    /**
     * 获取月份最后一天的年月日
     *
     * @param yearMonth: 'yyyy-MM'
     * @return string
     */
    public static String getLastDayOfMonth(String yearMonth) {
        int year = Integer.parseInt(yearMonth.split("-")[0]);  //年
        int month = Integer.parseInt(yearMonth.split("-")[1]); //月
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        Locale locale1 = new Locale("zh", "CN");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", locale1);
        return sdf.format(cal.getTime());
    }

    /**
     * 获取指定月份下个月第二天Date
     *
     * @param dateStr : "yyyy-MM"
     * @return
     */
    public static String getNextSecondDayByMonth(String dateStr) {
        Date date = DateUtils.stringToDate(dateStr + "-02", DATE_PATTERN);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        return DateUtils.format(calendar.getTime(), DATE_PATTERN);

    }

    /**
     * 获取本月第一天和最后一天的年月日
     *
     * @return List<Date>,list.get(0)是startDate，list.get(1)是endDate
     */
    public static List<Date> thisMonth() {
        List<Date> list = new ArrayList<Date>();
        Date startDate, endDate = null;
        //获取当前月第一天：
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        c = startZero(c);
        startDate = c.getTime();
        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        ca = endTwoThree(ca);
        endDate = ca.getTime();
        list.add(startDate);
        list.add(endDate);
        return list;
    }

    /**
     * 获取下月第一天和最后一天的年月日
     *
     * @return List<Date>,list.get(0)是startDate，list.get(1)是endDate
     */
    public static List<Date> nextMonth() {
        List<Date> list = new ArrayList<Date>();
        Date startDate, endDate = null;
        //获取当前月第一天：
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 1);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        c = startZero(c);
        startDate = c.getTime();
        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.MONTH, 1);
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        ca = endTwoThree(ca);
        endDate = ca.getTime();
        list.add(startDate);
        list.add(endDate);
        return list;
    }

    /**
     * 本年的起始日期，结束日期
     *
     * @return List<Date>,list.get(0)是startDate，list.get(1)是endDate
     */
    public static List<Date> thisYear() {
        List<Date> list = new ArrayList<Date>();
        Date startDate, endDate = null;
        // 获取当前月第一天：
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, 0);
        c.set(Calendar.DAY_OF_YEAR, 1);// 设置为1号,当前日期既为本年第一天
        c = DateUtils.startZero(c);
        startDate = c.getTime();
        // 获取当前年最后一天
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_YEAR, ca.getActualMaximum(Calendar.DAY_OF_YEAR));
        ca = DateUtils.endTwoThree(ca);
        endDate = ca.getTime();
        list.add(startDate);
        list.add(endDate);
        return list;
    }

    /**
     * 获取最近指定天数的起始、终止日期，常用于查询条件“近一周”、“近两周”等
     * 以当前日期为终止日期endDate，往前数num天作为起始日期startDate，
     *
     * @param num(整数)
     * @return List<Date>,list.get(0)是startDate，list.get(1)是endDate
     */
    public static List<Date> nearDays(String num) {
        if (num == null) {
            return null;
        }
        List<Date> list = new ArrayList<Date>();
        Date startDate, endDate = null;
        Calendar calender = Calendar.getInstance();
        calender = endTwoThree(calender);
        endDate = calender.getTime();
        calender.add(Calendar.DATE, Integer.parseInt("-" + num));
        calender = startZero(calender);
        startDate = calender.getTime();
        list.add(startDate);
        list.add(endDate);
        return list;
    }

    /**
     * 通用查询条件处理
     *
     * @param param 查询本周时param=101，本月时param=102；近一周，近两周等固定天数时param=天数；自定义时间，param格式为"Fdate1Tdate2"
     * @return List<Date>;  list.get(0)是startDate，list.get(1)是endDate
     */
    public static List<Date> queryDateProcess(String param) {
        if (param == null) {
            return null;
        }
        List<Date> list = new ArrayList<Date>();
        if (param.startsWith("F")) {
            list = fromDateToDate(param);
        } else if (param.equals("101")) {//本周应完成
            list = Arrays.asList(getWeekStartAndEnd(0));
        } else if (param.equals("102")) {//本月应完成
            list = thisMonth();
        } else if (param.equals("103")) {//本年应完成
            list = thisYear();
        } else {
            list = nearDays(param);
        }
        return list;
    }

    /**
     * 设置时间从零点零分零秒开始
     *
     * @param c
     * @return Calendar
     */
    public static Calendar startZero(Calendar c) {
        if (c == null) {
            return null;
        }
        c.set(Calendar.HOUR_OF_DAY, 0);//
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c;
    }

    /**
     * 设置时间23点59分59秒结束
     *
     * @param c
     * @return Calendar
     */
    public static Calendar endTwoThree(Calendar c) {
        if (c == null) {
            return null;
        }
        c.set(Calendar.HOUR_OF_DAY, 23);//
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 0);
        return c;
    }

    /**
     * 将开始日至结束日的日期转换成list<Date>
     *
     * @param start 开始日期
     * @param end   结束日期
     * @return
     */
    public static List<java.sql.Date> dateSplit(Date start, Date end) {
        Long spi = end.getTime() - start.getTime();
        Long step = spi / (24 * 60 * 60 * 1000);// 相隔天数
        List<java.sql.Date> dateList = new ArrayList<java.sql.Date>();
        dateList.add(new java.sql.Date(start.getTime()));//从1月1日开始
        for (int i = 1; i <= step; i++) {
            dateList.add(new java.sql.Date(dateList.get(i - 1).getTime() + (24 * 60 * 60 * 1000)));// 比上一天加一
        }
        return dateList;
    }

    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 根据日期返回周几
     *
     * @param date 日期，格式"yyyy-MM-dd"
     * @return
     */
    public static String change(Date date) {
        if (date == null) {
            return null;
        }
        String w = null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int i = cal.get(Calendar.DAY_OF_WEEK);
        switch (i) {
            case 1:
                w = "周日";
                break;
            case 2:
                w = "周一";
                break;
            case 3:
                w = "周二";
                break;
            case 4:
                w = "周三";
                break;
            case 5:
                w = "周四";
                break;
            case 6:
                w = "周五";
                break;
            case 7:
                w = "周六";
                break;
        }
        return w;
    }

    /**
     * 判断date是否在fromDate,toDate之间
     *
     * @param fromDate
     * @param toDate
     * @param date
     * @return 默认date不在fromDate与toDate之间
     */
    public static boolean comparDateIsInner(Date fromDate, Date toDate, Date date) {
        boolean flag = false;
        //转成SQL的date，按天比较
        java.sql.Date fd = new java.sql.Date(fromDate.getTime());
        java.sql.Date td = new java.sql.Date(toDate.getTime());
        java.sql.Date d = new java.sql.Date(date.getTime());
        long i = d.getTime() - fd.getTime();
        long j = td.getTime() - d.getTime();
        if (i >= 0 && j >= 0) {
            flag = true;
        }
        return flag;
    }

    /**
     * 比较两个日期是否相同
     *
     * @param da "yyyy-MM-dd"
     * @param dt "yyyy-MM-dd"
     * @return 相同返回true，不相同返回false
     */
    public static boolean comparDateIsEqual(String da, String dt) {
        boolean flag = false;
        if (da == null || dt == null) {
            return flag;
        }
        if (da.equals(dt)) {
            flag = true;
        }
        return flag;
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getYearLast(int year) {
        Locale locale1 = new Locale("zh", "CN");
        SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN, locale1);
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();

        return currYearLast;
    }

    /**
     * 获取考勤周期（上月20号到本月19号）
     *
     * @return List<Date>,list.get(0)是startDate，list.get(1)是endDate
     * @throws ParseException
     */
    public static List<Date> getAttendanceCycle(String date) throws ParseException {
        Locale locale1 = new Locale("zh", "CN");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", locale1);
        Date now = format.parse(date);//将字符串转为时间
        List<Date> list = new ArrayList<Date>();
        Date startDate, endDate = null;
        //判断时间是否超过20号
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.set(Calendar.DAY_OF_MONTH, 20);
        if (now.getTime() < calendar.getTimeInMillis()) {
            //如果没超过20号，周期是上月20号到当月19号
            Calendar c = Calendar.getInstance();
            c.setTime(now);
            c.add(Calendar.MONTH, -1);
            c.set(Calendar.DAY_OF_MONTH, 20);//获取上月20号

            c = startZero(c);
            startDate = c.getTime();
            //获取当前月最后一天
            Calendar ca = Calendar.getInstance();
            ca.setTime(now);
            ca.set(Calendar.DAY_OF_MONTH, 20); //获取本月19号
            ca = startZero(ca);
            endDate = ca.getTime();
            list.add(startDate);
            list.add(endDate);

        } else {
            //如果大于等于20号，周期为当月20号到下月19号
            Calendar c = Calendar.getInstance();
            c.setTime(now);
            c.set(Calendar.DAY_OF_MONTH, 20);//获取本月20号

            c = startZero(c);
            startDate = c.getTime();
            //获取当前月最后一天
            Calendar ca = Calendar.getInstance();
            ca.setTime(now);
            ca.add(Calendar.MONTH, 1);
            ca.set(Calendar.DAY_OF_MONTH, 20); //获取下月19号
            ca = startZero(ca);
            endDate = ca.getTime();
            list.add(startDate);
            list.add(endDate);

        }

        return list;
    }


    /**
     * 判断时间格式 格式必须为“YYYY-MM-dd”
     * 2004-2-30 是无效的
     * 2003-2-29 是无效的
     *
     * @param str
     * @return boolean
     */
    public static boolean isValidDate(String str, String pattern) {
        //String str = "2007-01-02";
        Locale locale1 = new Locale("zh", "CN");
        DateFormat formatter = new SimpleDateFormat(pattern, locale1);
        try {
            Date date = (Date) formatter.parse(str);
            return str.equals(formatter.format(date));
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 描述:获取当前月份为准前后n月的第一天，n为负，前，n为正，后，n为0,本月
     *
     * @return
     */
    public static String getSecondDayOfMonth(int n) {
        Locale locale1 = new Locale("zh", "CN");
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd", locale1);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, n);
        calendar.set(Calendar.DAY_OF_MONTH, 2);
        return dft.format(calendar.getTime());
    }

    /**
     * 描述:获取明天日期
     *
     * @return
     */
    public static String getTomorrow() {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        Locale locale1 = new Locale("zh", "CN");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", locale1);
        return formatter.format(date);
    }

    /**
     * 获取上月某日和本月某日
     *
     * @return List<Date>,list.get(0)是startDate，list.get(1)是endDate
     * @throws ParseException
     */
    public static List<Date> getLastMonthDayAndThisMonthDay(int lastMonthDay, int thisMonthDay) throws ParseException {
        Locale locale1 = new Locale("zh", "CN");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", locale1);
        Date now = new Date();//将字符串转为时间
        List<Date> list = new ArrayList<Date>();
        Date startDate, endDate = null;
        //判断时间是否超过20号
        Calendar c = Calendar.getInstance();
        c.setTime(now);
        c.add(Calendar.MONTH, -1);
        c.set(Calendar.DAY_OF_MONTH, lastMonthDay);//获取上月20号
        c = startZero(c);
        startDate = c.getTime();
        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();
        ca.setTime(now);
        ca.set(Calendar.DAY_OF_MONTH, thisMonthDay); //获取本月19号
        ca = endTwoThree(ca);
        endDate = ca.getTime();
        list.add(startDate);
        list.add(endDate);
        return list;
    }

    /**
     * 获取下个月的某天
     */
    public static List<Date> getNextMonthDay(int day) throws ParseException {
        Locale locale1 = new Locale("zh", "CN");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", locale1);
        Date now = new Date();//将字符串转为时间
        List<Date> list = new ArrayList<Date>();
        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();
        ca.setTime(now);
        ca.add(Calendar.MONTH, 1);
        ca.set(Calendar.DAY_OF_MONTH, day); //获取本月19号
        ca = endTwoThree(ca);
        Date endDate = ca.getTime();
        list.add(endDate);
        return list;//返回
    }

    /**
     * 将时间改为0点0分0秒
     */
    public static Date setDateZero(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c = startZero(c);
        date = c.getTime();
        return date;
    }

    /**
     * 将时间改为23点59分59秒
     */
    public static Date setDateTwoThree(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c = endTwoThree(c);
        date = c.getTime();
        return date;
    }

    /**
     * 获取指定年,指定季度的第一天和最后一天
     *
     * @param year
     * @param quarter
     * @return List<Date> list.get(0)->第一天 list.get(1)-最后一天
     */
    public static List<Date> getYearQuarterFirstAndEndDay(String year, String quarter) {
        if (StringUtils.isNotBlank(year) && StringUtils.isNotBlank(quarter)) {
            Calendar calendar = Calendar.getInstance();
            List<Date> dateList = new ArrayList<>();
            Integer startMonth = new Integer(0);
            Integer endMonth = new Integer(0);
            if ("01".equals(quarter)) {
                startMonth = 1 - 1;
                endMonth = 3 - 1;
            } else if ("02".equals(quarter)) {
                startMonth = 4 - 1;
                endMonth = 6 - 1;
            } else if ("03".equals(quarter)) {
                startMonth = 7 - 1;
                endMonth = 9 - 1;
            } else if ("04".equals(quarter)) {
                startMonth = 10 - 1;
                endMonth = 12 - 1;
            } else {
                startMonth = calendar.get(Calendar.MONTH);
                endMonth = calendar.get(Calendar.MONTH);
            }
            calendar.set(Integer.parseInt(year), startMonth, 1);
            dateList.add(calendar.getTime());
            calendar.set(Integer.parseInt(year), endMonth, 1);
            calendar.roll(Calendar.DATE, -1);
            dateList.add(calendar.getTime());
            return dateList;
        } else {
            return null;
        }
    }

    /**
     * 通过开始日期和结束日期获取季度值
     *
     * @param day 2020-01-01,2020-03-31
     * @return String 格式：202001
     */
    public static String getYearQuarterByStartEndDay(String day) {
        if (StringUtils.isNotBlank(day)) {
            //03 06 09 12
            String[] date = day.split(",");
            String month = date[1].substring(5, 7);
            String quarter = "";
            switch (month) {
                case "03":
                    quarter = "01";
                    break;
                case "06":
                    quarter = "02";
                    break;
                case "09":
                    quarter = "03";
                    break;
                case "12":
                    quarter = "04";
                    break;
            }
            return date[1].substring(0, 4) + quarter;
        } else {
            return format(new Date()).replaceAll("-", "").substring(0, 6);
        }
    }

    public static String getNextYear() {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        //获取下一年
        calendar.add(Calendar.YEAR, 1);
        return String.valueOf(calendar.get(Calendar.YEAR));
    }

    public static String getCurrYear() {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        return String.valueOf(calendar.get(Calendar.YEAR));
    }

    /**
     * 校验日期格式
     * @param dateStr 日期字符串
     * 格式 默认 yyyy-MM-dd
     * @return
     */
    public static boolean checkDateFormat(String dateStr) {
        return checkDateFormat(dateStr, "yyyy-MM-dd");
}
    /**
     * 校验日期格式
     * @param dateStr 日期字符串
     * @param format 格式
     * @return
     */
    public static boolean checkDateFormat(String dateStr, String format) {
        if (StringUtils.isBlank(dateStr)) {
            return true;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        // 设置为严格模式，不容忍不合法的日期
        sdf.setLenient(false);

        try {
            Date date = sdf.parse(dateStr);
            // 如果解析成功，且解析后的日期和原日期字符串相同，则日期合法
            return dateStr.equals(sdf.format(date));
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * 转换日期格式 默认格式 yyyy-MM-dd
     * @param dateStr 日期字符串
     * @return 返回转换好的字符串
     */
    public static String convertDateFormat(String dateStr) {
        return convertDateFormat(dateStr, "yyyy-MM-dd");
    }

    /**
     * 转换日期格式
     * @param dateStr 日期字符串
     * @param targetFormat 转换的格式
     * @return 返回转换好的字符串
     */
    public static String convertDateFormat(String dateStr, String targetFormat) {
        String[] possibleFormats = {"yyyyMMdd", "yyyy/MM/dd"}; // Add more formats as needed

        for (String format : possibleFormats) {
            try {
                java.time.format.DateTimeFormatter sourceFormatter = java.time.format.DateTimeFormatter.ofPattern(format);
                java.time.LocalDate date = java.time.LocalDate.parse(dateStr, sourceFormatter);

                java.time.format.DateTimeFormatter targetFormatter = java.time.format.DateTimeFormatter.ofPattern(targetFormat);
                return date.format(targetFormatter);
            } catch (DateTimeParseException e) {
                return null;
            }
        }
        return null;
    }
}

